#---------------------------------------------------------------------
# Makefile for Greenplum Database
#---------------------------------------------------------------------

# keep the default target above any targets from included makefiles
all : devel

#---------------------------------------------------------------------
# Declare the targets
#---------------------------------------------------------------------

# Targets for building GPDB with various options
# (development builds)
.PHONY : devel devel_faultinj

# (release builds)
.PHONY : dist dist_prof

# Targets recognized by the PostgreSQL makefile (GNUmakefile)
.PHONY : all install installdirs uninstall distprep
.PHONY : clean distclean maintainer-clean

# The pvk target builds a subset of the GPDB package that includes just the
# tools used to verify the configuration of a set of GPDB servers.
.PHONY : pvk

# Targets for running autoconf and autoheader
.PHONY : autoconf

# Internal functions which are invoked by other rules within this makefile
.PHONY : copydocs mgmtcopy copylibs
.PHONY : greenplum_path RECONFIG HOMEDEP GPROOTDEP GPROOTDEP GPROOTFAIL
.PHONY : gccVersionCheck clients

#---------------------------------------------------------------------
# Make 3rd-party libraries and tools
#---------------------------------------------------------------------

# tell included makefiles where to find build-top-level resources
BLD_TOP:=$(shell sh -c pwd)
include Makefile.global


ifneq "$(BLD_BITS)" ""
export NO_M64=1
endif

#$(warning )
#$(warning PATH=$(PATH))
#$(warning LD_LIBRARY_PATH=$(LD_LIBRARY_PATH))
#$(warning )
#$(warning BLD_ARCH=$(BLD_ARCH))
#$(warning BLD_BITS=$(BLD_BITS))
#$(warning BLD_CFLAGS=$(BLD_CFLAGS))
#$(warning )

BLD_CXX_LIB_DIR=$($(BLD_ARCH)_CXX_LIB_DIR)

#---------------------------------------------------------------------
# Directory paths and file names
#---------------------------------------------------------------------

GPPGDIR=$(shell cd .. && pwd)
GPMGMT=../gpMgmt
GPPERFMON=gpperfmon
PLATFORM=platform
BUILDDIR=$(GPPGDIR)

RELEASE=devel
GPDIR=greenplum-db-$(RELEASE)
PLRDIR=greenplum-plr-$(RELEASE)
CLIENTSDIR=greenplum-clients-$(RELEASE)
GPDOCDIR=$(CURDIR)/docs/release

DISTPATH=$(GPROOT)/$(GPDIR)
CLIENTSDISTPATH=$(GPROOT)/$(CLIENTSDIR)

ifeq "$(DEVPATH)" ""
BLD_HOME=$(HOME)
DEVPATH=$(BLD_HOME)/$(GPDIR)
CLIENTSDEVPATH=$(BLD_HOME)/$(CLIENTSDIR)
else
# DEVPATH has been passed in as by the build scripts
GPROOT_DEV=$(dir $(DEVPATH))
CLIENTSDEVPATH=$(GPROOT_DEV)$(CLIENTSDIR)
endif

# files generated by 'configure'
ISCONFIG=$(GPPGDIR)/GNUmakefile


##
## On these platforms, we do the full build including the server. On other
## platforms, we do a client-only build.
##
SERVER_PLATFORMS=rhel7_x86_64 rhel6_x86_64 rhel8_x86_64 rocky8_x86_64 linux_x86_64

#---------------------------------------------------------------------
# Compiler options
#---------------------------------------------------------------------

OPTFLAGS="$(strip $(BLD_CFLAGS) -O3 -fargument-noalias-global -fno-omit-frame-pointer -g)"
PROFFLAGS="$(strip $(BLD_CFLAGS) -O3 -fargument-noalias-global -fno-omit-frame-pointer -g)"

ifeq (on, ${GPDBGOPT})
CFLAGS_OPT=-O1 -fno-omit-frame-pointer
else
CFLAGS_OPT=-O0
endif

ifeq (on, ${GPDBGCOV})
DEBUG_CFLAGS_OPT=
else
DEBUG_CFLAGS_OPT=-g3
endif

DEBUGFLAGS="$(strip $(BLD_CFLAGS) $(CFLAGS_OPT) $(DEBUG_CFLAGS_OPT))"

#---------------------------------------------------------------------
# configure
#---------------------------------------------------------------------

DEFPORT=5432

ORCA_CONFIG=--enable-orca

rhel6_x86_64_CONFIGFLAGS=--with-quicklz  --enable-gpperfmon --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --with-libxml --with-pythonsrc-ext --with-uuid=e2fs
rhel7_x86_64_CONFIGFLAGS=--with-quicklz  --enable-gpperfmon --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --with-libxml --with-pythonsrc-ext --with-uuid=e2fs
rhel8_x86_64_CONFIGFLAGS=--with-quicklz  --disable-gpperfmon --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --with-libxml --with-pythonsrc-ext --with-uuid=e2fs
rocky8_x86_64_CONFIGFLAGS=--with-quicklz --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --enable-gpcloud --with-libxml --with-openssl --with-pam --with-ldap --with-pythonsrc-ext --with-uuid=e2fs
rocky9_x86_64_CONFIGFLAGS=--with-quicklz --disable-gpperfmon --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --with-libxml --with-pythonsrc-ext --with-uuid=e2fs
rhel9_x86_64_CONFIGFLAGS=--with-quicklz  --disable-gpperfmon --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --with-libxml --with-pythonsrc-ext --with-uuid=e2fs
linux_x86_64_CONFIGFLAGS=${ORCA_CONFIG} --with-libxml --with-pythonsrc-ext --with-uuid=e2fs
ubuntu18.04_x86_64_CONFIGFLAGS=--with-quicklz --enable-gpperfmon --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --with-libxml --with-pythonsrc-ext --with-uuid=e2fs
ubuntu20.04_x86_64_CONFIGFLAGS=--with-quicklz --enable-gpperfmon --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --with-libxml --with-pythonsrc-ext --with-uuid=e2fs
sles12_x86_64_CONFIGFLAGS=--with-quicklz --enable-gpperfmon --with-gssapi --enable-mapreduce --enable-orafce --enable-ic-proxy ${ORCA_CONFIG} --with-libxml --with-pythonsrc-ext --with-uuid=e2fs
BLD_CONFIGFLAGS=$($(BLD_ARCH)_CONFIGFLAGS)

CONFIGFLAGS=$(strip $(BLD_CONFIGFLAGS) --with-pgport=$(DEFPORT) $(BLD_DEPLOYMENT_SETTING))

ifdef CONFIGURE_FLAGS
CONFIGFLAGS+= $(CONFIGURE_FLAGS)
endif

ifneq (false, ${PG_LANG})
CONFIGFLAGS+= --with-perl --with-python
ifdef TCL_CFG
CONFIGFLAGS+= --with-tcl-config=${TCL_CFG}
endif
endif

# Configure in thirdparty libraries from the ext/ directory
CONFIG_INCLUDES=$(BLD_THIRDPARTY_INCLUDE_DIR)
CONFIG_LIBRARIES=$(strip $(BLD_THIRDPARTY_LIB_DIR) $(BLD_CXX_LIB_DIR))
CONFIGFLAGS+= --with-openssl --with-pam --with-ldap --with-includes="$(CONFIG_INCLUDES)" --with-libraries="$(CONFIG_LIBRARIES)"

CONFIG_ADDITIONS=$($(BLD_ARCH)_CONFIG_ADDITIONS)

CONFIGFLAGS+= $(CONFIG_ADDITIONS)
CONFIGFLAGS+= --disable-rpath
CONFIGFLAGS+= LDFLAGS='-Wl,--enable-new-dtags -Wl,-rpath,\$$$$ORIGIN/../lib'

RECONFIG :
	rm -f Debug/GNUmakefile
	rm -f Release/GNUmakefile
	rm -f $(GPPGDIR)/GNUmakefile

$(GPPGDIR)/GNUmakefile : $(GPPGDIR)/configure  env.sh
	rm -rf $(INSTLOC)
	mkdir -p $(GPPGDIR)
	echo "Running ./configure with CONFIGFLAGS=$(CONFIGFLAGS)\n"
	cd $(GPPGDIR) && CC="$(strip $(BLD_CC) $(BLD_CFLAGS))"    \
                     CFLAGS=$(INSTCFLAGS) 					  \
                     ./configure $(CONFIGFLAGS)               \
                         --prefix=$(INSTLOC)                  \
                         --mandir=$(INSTLOC)/man

Debug/GNUmakefile : $(GPPGDIR)/configure  env.sh
	rm -rf $(INSTLOC)
	mkdir -p Debug
	echo "Running ./configure with CONFIGFLAGS=$(CONFIGFLAGS)\n"
	cd Debug && CC="$(strip $(BLD_CC) $(BLD_CFLAGS))"        \
                CFLAGS=$(INSTCFLAGS)					     \
                ./configure $(CONFIGFLAGS)                   \
                    --prefix=$(INSTLOC)                      \
                    --mandir=$(INSTLOC)/man

Release/GNUmakefile : $(GPPGDIR)/configure  env.sh
	rm -rf $(INSTLOC)
	mkdir -p Release
	echo "Running ./configure with CONFIGFLAGS=$(CONFIGFLAGS)\n"
	cd Release && CC="$(strip $(BLD_CC) $(BLD_CFLAGS))"     \
                  CFLAGS=$(INSTCFLAGS) 	                    \
                  ./configure $(CONFIGFLAGS)                \
                      --prefix=$(INSTLOC)                   \
                      --mandir=$(INSTLOC)/man

#---------------------------------------------------------------------
# autoconf
#---------------------------------------------------------------------
# Whenever there is a change to the configure.in file, autoconf and
# autoheader should be run to regenerate the files configure and
# src/include/pg_config.h.in.  Note that autoconf and autoheader
# don't update the timestamps of their output files unless there is an
# actual change in the contents; thus 'make' rules don't work well for
# rebuilding those files automatically.  Developers should rebuild them
# manually when needed, which can be done by invoking 'make autoconf'.

autoconf : RECONFIG
	cd $(GPPGDIR) && autoconf && autoheader

#---------------------------------------------------------------------
# standardized build rules
#---------------------------------------------------------------------

BLD_LD_LIBRARY_PATH:=$($(BLD_ARCH)_LD_LIBRARY_PATH)

BLD_GPDB_BUILDSET=$($(BLD_ARCH)_GPDB_BUILDSET)
perl_archlibexp:=$(shell perl -MConfig -e 'print $$Config{archlibexp}')

# set default build steps
define BUILD_STEPS
	@rm -rf $(INSTLOC)
	cd $(BUILDDIR) && PYGRESQL_LDFLAGS='-Wl,-rpath,\$$$$ORIGIN/../../../lib -Wl,--enable-new-dtags' QUICKLZ_LDFLAGS='-Wl,-rpath,\$$$$ORIGIN/../../lib -Wl,--enable-new-dtags' $(MAKE) $(PARALLEL_MAKE_OPTS) install
	cd $(BUILDDIR)/src/pl/plpython && $(MAKE) clean && echo 'LDFLAGS += -Wl,-rpath,\$$$$ORIGIN/../../ext/python/lib/ -Wl,--enable-new-dtags' >> Makefile && $(MAKE) $(PARALLEL_MAKE_OPTS) install && cd $(BUILDDIR)
	cd $(BUILDDIR)/src/pl/plperl && $(MAKE) clean && echo "LDFLAGS += -Wl,-rpath,$(perl_archlibexp)/CORE -Wl,--enable-new-dtags" >> GNUmakefile && echo "LDFLAGS_SL += -Wl,-rpath,$(perl_archlibexp)/CORE -Wl,--enable-new-dtags" >> GNUmakefile && $(MAKE) $(PARALLEL_MAKE_OPTS) install && cd $(BUILDDIR)
	#@$(MAKE) mgmtcopy INSTLOC=$(INSTLOC)
	@$(MAKE) mkpgbouncer INSTLOC=$(INSTLOC) BUILDDIR=$(BUILDDIR)
	@$(MAKE) copydocs INSTLOC=$(INSTLOC)
	@$(MAKE) copylibs INSTLOC=$(INSTLOC)
	@$(MAKE) greenplum_path INSTLOC=$(INSTLOC)
	@$(MAKE) clients INSTLOC=$(INSTLOC) CLIENTSINSTLOC=$(CLIENTSINSTLOC)
	@$(MAKE) set_scripts_version INSTLOC=$(CLIENTSINSTLOC)
	@$(MAKE) copylicense INSTLOC=$(INSTLOC) \
                         CLIENTSINSTLOC=$(CLIENTSINSTLOC)
endef

ifeq "$(BLD_GPDB_BUILDSET)" "partial"
define BUILD_STEPS
	rm -rf $(INSTLOC)
	cd $(BUILDDIR)/gpMgmt/ && $(MAKE) generate_greenplum_path_file
	cd $(BUILDDIR)/src/backend/ && $(MAKE) ../../src/include/parser/gram.h
	cd $(BUILDDIR)/src/backend/ && $(MAKE) ../../src/include/utils/errcodes.h
	cd $(BUILDDIR)/src/backend/ && $(MAKE) ../../src/include/utils/fmgroids.h
	cd $(BUILDDIR)/src/port/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/scripts/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/psql/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/pg_config/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/pg_dump/ && $(MAKE) pg_dump
	cp -p $(BUILDDIR)/src/bin/pg_dump/pg_dump $(INSTLOC)/bin/
	cd $(BUILDDIR)/src/bin/pg_dump/ && $(MAKE) pg_dumpall
	cp -p $(BUILDDIR)/src/bin/pg_dump/pg_dumpall $(INSTLOC)/bin/
	cd $(BUILDDIR)/src/bin/pg_dump/ && $(MAKE) pg_restore
	cp -p $(BUILDDIR)/src/bin/pg_dump/pg_restore $(INSTLOC)/bin/
	cd $(BUILDDIR)/src/interfaces/ecpg/ && $(MAKE) install
	cd $(BUILDDIR)/src/include/ && $(MAKE) install
	cd $(BUILDDIR)/src/interfaces/libpq/ && $(MAKE) install
	cd $(BUILDDIR)/src/bin/gpfdist && $(MAKE) install
	cp -p $(GPMGMT)/bin/gpload $(INSTLOC)/bin/gpload
	cp -p $(GPMGMT)/bin/gpload.py $(INSTLOC)/bin/gpload.py
	$(MAKE) copylibs INSTLOC=$(INSTLOC)
	cd $(GPMGMT)/bin && $(MAKE) pygresql INSTLOC=$(INSTLOC)
	$(MAKE) clients INSTLOC=$(INSTLOC) CLIENTSINSTLOC=$(CLIENTSINSTLOC)
endef
endif

#---------------------------------------------------------------------
# environment for later builds inside the source tree
#
# run this to create an environment-loading script, source it,
# and be able to build inside the source tree with the same
# results as building from here at the top
#---------------------------------------------------------------------

env.sh:
	echo "export BLD_ARCH=$(BLD_ARCH)" > env.sh
	echo "export NO_M64=$(NO_M64)" >> env.sh
	echo "export CC=\"$(strip $(BLD_CC) $(BLD_CFLAGS))\"" >> env.sh
	echo "export PATH=$(PATH)" >> env.sh
ifneq "$(PERL_DIR)" ""
	echo "export PERL5LIB=$(PERL_DIR)/lib/site_perl/5.12.4/x86_64-linux:$(PERL_DIR)/lib/site_perl/5.12.4:$(PERL_DIR)/lib/5.12.4/x86_64-linux:$(PERL_DIR)/lib/5.12.4" >> env.sh
endif

#---------------------------------------------------------------------
# Development builds
#---------------------------------------------------------------------

devel : INSTCFLAGS=$(DEBUGFLAGS)
devel : INSTLOC=$(DEVPATH)
devel : CLIENTSINSTLOC=$(CLIENTSDEVPATH)
devel : CONFIGFLAGS+= --enable-cassert --enable-debug --enable-depend
ifdef ENABLE_VPATH_BUILD
devel : BUILDDIR=Debug
devel : ISCONFIG=$(BUILDDIR)/GNUmakefile
devel : gccVersionCheck HOMEDEP Debug/GNUmakefile
else
devel : gccVersionCheck HOMEDEP $(ISCONFIG)
endif
	$(BUILD_STEPS)

#---------------------------------------------------------------------
# Release builds
#---------------------------------------------------------------------

dist : INSTCFLAGS=$(OPTFLAGS)
dist : INSTLOC=$(DISTPATH)
dist : CLIENTSINSTLOC=$(CLIENTSDISTPATH)
ifdef ENABLE_VPATH_BUILD
dist : BUILDDIR=Release
dist : ISCONFIG=$(BUILDDIR)/GNUmakefile
dist : gccVersionCheck GPROOTDEP RECONFIG Release/GNUmakefile
else
dist : gccVersionCheck GPROOTDEP RECONFIG $(ISCONFIG)
endif
	$(BUILD_STEPS)

dist_prof : INSTCFLAGS=$(PROFFLAGS)
dist_prof : INSTLOC=$(DISTPATH)
dist_prof : CLIENTSINSTLOC=$(CLIENTSDISTPATH)
ifdef ENABLE_VPATH_BUILD
dist_prof : BUILDDIR=Release
dist_prof : ISCONFIG=$(BUILDDIR)/GNUmakefile
dist_prof : gccVersionCheck GPROOTDEP RECONFIG $(ISCONFIG)
else
dist_prof : ISCONFIG=../GNUmakefile
dist_prof : gccVersionCheck GPROOTDEP RECONFIG $(ISCONFIG)
endif
	$(BUILD_STEPS)

dist_faultinj : CLIENTSINSTLOC=$(CLIENTSDISTPATH)
ifdef ENABLE_VPATH_BUILD
dist_faultinj : BUILDDIR=Release
dist_faultinj : ISCONFIG=$(BUILDDIR)/GNUmakefile
dist_faultinj : gccVersionCheck GPROOTDEP RECONFIG Release/GNUmakefile
else
dist_faultinj : ISCONFIG=../GNUmakefile
dist_faultinj : gccVersionCheck GPROOTDEP RECONFIG $(ISCONFIG)
endif
	$(BUILD_STEPS)

#---------------------------------------------------------------------
# clientTools
#---------------------------------------------------------------------
#
# The clients target builds GPDB [f]or a subset of files needed for client
# platforms.  It also builds clientTools-specific items like the ODBC drivers.
#

ifneq "$(GPROOT)" ""
CLIENTS_HOME_DIR=$(GPROOT)
else
CLIENTS_HOME_DIR=$(BLD_HOME)
endif
CLIENTSINSTLOC=$(CLIENTS_HOME_DIR)/greenplum-clients
CLIENTSINSTLOC_BIN=$(CLIENTSINSTLOC)/bin
CLIENTSINSTLOC_BINEXT=$(CLIENTSINSTLOC)/bin/ext/
CLIENTSINSTLOC_EXT=$(CLIENTSINSTLOC)/ext
CLIENTSINSTLOC_LIB=$(CLIENTSINSTLOC)/lib
CLIENTSINSTLOC_LIB_PWARE=$(CLIENTSINSTLOC)/lib/pware
CLIENTSINSTLOC_DOCS=$(CLIENTSINSTLOC)/docs
CLIENTSINSTLOC_OPENSOURCE=$(CLIENTSINSTLOC)/thirdparty

DOC_TYPE=Unix
SCRIPT=.sh

define tmpCLIENTS_FILESET_BIN
	gpfdist
	gpload
	gpload.py
	pg_dump
	pg_dumpall
	psql
	createdb
	dropdb
	createlang
	droplang
	createuser
	dropuser
endef
CLIENTS_FILESET_BIN = $(strip $(tmpCLIENTS_FILESET_BIN))

define tmpCLIENTS_FILESET_BINEXT
	pygresql
	yaml
endef
CLIENTS_FILESET_BINEXT = $(strip $(tmpCLIENTS_FILESET_BINEXT))


BLD_OS:=$(shell uname -s)
Darwin_CLIENTS_LIBS=libcrypto.*.dylib libssl.*.dylib libpq.*.dylib* libkrb5.*.dylib libcom_err.*.dylib libldap_r-*.dylib libk5crypto.*.dylib libkrb5support.*.dylib liblber-*.dylib libyaml*.dylib
Linux_CLIENTS_LIBS=libpq.so*
define tmpCLIENTS_FILESET_LIB
	$($(BLD_OS)_CLIENTS_LIBS)
endef
CLIENTS_FILESET_LIB = $(strip $(tmpCLIENTS_FILESET_LIB))


ifeq "$(MPP_ARCH)" ""
rhel6_x86_64_MPP_ARCH=RHEL6-x86_64
rhel7_x86_64_MPP_ARCH=RHEL7-x86_64
rhel8_x86_64_MPP_ARCH=RHEL8-x86_64
rocky8_x86_64_MPP_ARCH=ROCKY8-x86_64
ifneq "$($(BLD_ARCH)_MPP_ARCH)" ""
export MPP_ARCH=$($(BLD_ARCH)_MPP_ARCH)
else
export MPP_ARCH=$(BLD_ARCH)
endif
endif

ifeq "$(INSTLOC)" ""
INSTLOC=$(GPDIR)
endif

VERSION:=$(shell [ -f ../VERSION ] && perl -pe 's, ,-,g' ../VERSION)
VERSION_SHORT:=$(shell [ -f ../VERSION ] && perl -pe 's, .*,,g' ../VERSION)

clients :
	if [ ! -z "$(CLIENTSINSTLOC)" ] && [ "$(CLIENTSINSTLOC)" != "/" ]; then rm -rf $(CLIENTSINSTLOC); fi
ifeq "$(findstring clients,$(BLD_TARGETS))" ""
	# ---- build and packaging of the clients package is currently disabled;
	# ---- set BLD_TARGETS="clients" to enable them.
else
	# ---- copy GPDB fileset ----
	mkdir -p $(CLIENTSINSTLOC_BIN)
	(cd $(INSTLOC)/bin/ && tar cf - $(CLIENTS_FILESET_BIN)) | (cd $(CLIENTSINSTLOC_BIN)/ && tar xpf -)$(check_pipe_for_errors)
	mkdir -p $(CLIENTSINSTLOC_BINEXT)
	(cd $(GPMGMT)/bin/ext/ && tar cf - $(CLIENTS_FILESET_BINEXT)) | (cd $(CLIENTSINSTLOC_BINEXT)/ && tar xpf -)$(check_pipe_for_errors)
ifneq "$(PYTHONHOME)" ""
	mkdir -p $(CLIENTSINSTLOC_EXT)/python
	(cd $(PYTHONHOME) && tar cf - .) | (cd $(CLIENTSINSTLOC_EXT)/python/ && tar xpf -)$(check_pipe_for_errors)
endif
	mkdir -p $(CLIENTSINSTLOC_LIB)
 ifneq "$(CLIENTS_FILESET_LIB)" ""
	 (cd $(INSTLOC)/lib/ && tar cf - $(CLIENTS_FILESET_LIB)) | (cd $(CLIENTSINSTLOC_LIB)/ && tar xpf -)$(check_pipe_for_errors)
 endif
	# ---- copy scripts fileset ----
	mkdir -p $(CLIENTSINSTLOC)
	cp -f client/scripts/greenplum_$@_path$(SCRIPT) $(CLIENTSINSTLOC)/

	# ---- copy license files ----
	cp ${BLD_TOP}/../LICENSE $(CLIENTSINSTLOC)
	cp ${BLD_TOP}/../NOTICE $(CLIENTSINSTLOC)

	# ---- updating the version in required packages ----
	@$(MAKE) set_scripts_version INSTLOC=$(CLIENTSINSTLOC)
endif

#---------------------------------------------------------------------
# Platform Verification Kit (PVK)
#   formerly checkTools
#
# The pvk target builds a subset of the GPDB package that includes
# just the tools used to verify the configuration of a set of GPDB servers.
#---------------------------------------------------------------------

PVK_SCRIPTS = \
	bin/gpscp			\
	bin/gpssh			\
	bin/gpcheckperf			\
	$(NULL)

PVK_FILESET = \
	$(CHECKTOOLS_SCRIPTS)	\
	bin/lib/gplib.py	\
	bin/lib/pxssh.py	\
	bin/lib/pexpect.py	\
	bin/lib/multidd		\
	bin/lib/stream		\
	$(NULL)

pvk : INSTCFLAGS=$(OPTFLAGS)
pvk : INSTLOC=$(DISTPATH)
pvk : gccVersionCheck version GPROOTDEP
	cd $(GPMGMT)/bin && $(MAKE) stream
	mkdir -p $(INSTLOC)/bin/lib
	@for file in $(PVK_FILESET); do \
	    echo "cp -p $(GPMGMT)/$${file} $(INSTLOC)/$${file}"; \
	    cp -p $(GPMGMT)/$${file} $(INSTLOC)/$${file}; \
	done
	perl putversion $(foreach script,$(PVK_SCRIPTS),$(INSTLOC)/$(script))

#---------------------------------------------------------------------
# Set Version
#---------------------------------------------------------------------

SET_VERSION_SCRIPTS = \
	bin/gpactivatestandby \
	bin/gpaddmirrors \
	bin/gpcheckperf \
	bin/gpdeletesystem \
	bin/gpexpand \
	bin/gpfilespace \
	bin/gpinitstandby \
	bin/gpinitsystem \
	bin/gpload.py \
	bin/gplogfilter \
	bin/gpmovemirrors \
	bin/gprecoverseg \
	bin/gpreload \
	bin/gpscp \
	bin/gpssh \
	bin/gpssh-exkeys \
	bin/gpstart \
	bin/gpstate \
	bin/gpstop \
	bin/gpsys1 \
	bin/lib/gpcheckcat \
	sbin/gpconfig_helper.py \
	sbin/gpcleansegmentdir.py \
	sbin/gpgetstatususingtransition.py \
	sbin/gpsegstart.py \
	sbin/gpsegstop.py \
	sbin/gpupgrademirror.py \
	lib/python/gppylib/programs/clsAddMirrors.py \
	lib/python/gppylib/programs/clsRecoverSegment.py \
	lib/python/gppylib/programs/clsSystemState.py \
	lib/python/gppylib/programs/gppkg.py \
	lib/python/gppylib/programs/kill.py \
	lib/python/gppylib/mainUtils.py \
	$(NULL)

set_scripts_version :
	@for file in $(SET_VERSION_SCRIPTS); do \
	    if [ -f $(INSTLOC)/$${file} ]; then \
	        perl $(BLD_TOP)/../putversion $(INSTLOC)/$${file} ; \
	    fi ; \
	done

#---------------------------------------------------------------------
# Internal functions
#---------------------------------------------------------------------

mkpgbouncer:
ifeq "$(shell uname -s)" "Darwin"
	echo "pgbouncer can't build on Mac"
else
	@if [ ! -f extensions/pgbouncer/source/configure ]; then cd extensions/pgbouncer/source && ./autogen.sh ;fi
	@cd extensions/pgbouncer/source && ./configure --with-libevent=$(BLD_TOP)/ext/$(BLD_ARCH) --prefix=$(INSTLOC) --enable-evdns --with-pam --with-ldap
	$(MAKE) -C extensions/pgbouncer/source install
endif

copydocs :
	@if [ ! -d $($INSTLOC)/docs ] ; then mkdir -p $(INSTLOC)/docs; fi
	@if [ ! -d $(GPDOCDIR) ] ; then \
	    echo "Error Copying Documentation: $(GPDOCDIR) is not present"; \
	else  echo "Copying Documentation" ;  \
	    if [ -d $(INSTLOC)/doc/postgresql/contrib ] ; then \
	        mkdir -p $(INSTLOC)/docs/contrib ; \
	        echo "Copying postgresql contrib documentation" ; \
		cp -r $(INSTLOC)/doc/postgresql/contrib/* $(INSTLOC)/docs/contrib ; \
	    fi ; \
	fi
	@if [ -d $(INSTLOC)/doc ] ; then \
	    echo "Removing $(INSTLOC)/doc" ; \
	    rm -rf $(INSTLOC)/doc ; \
	fi

mgmtcopy :
	#Copy the management utilities
	mkdir -p $(INSTLOC)/bin
	mkdir -p $(INSTLOC)/lib
	mkdir -p $(INSTLOC)/lib/python
	mkdir -p $(INSTLOC)/sbin
	#Setup /lib/python contents
	cp -rp $(GPMGMT)/bin/gppylib $(INSTLOC)/lib/python
	cp -rp $(GPMGMT)/bin/ext/* $(INSTLOC)/lib/python
	cp -rp $(GPMGMT)/bin $(INSTLOC)
	cp -rp $(GPMGMT)/sbin/* $(INSTLOC)/sbin/.
	if [ ! -d ${INSTLOC}/docs ] ; then mkdir ${INSTLOC}/docs ; fi
	if [ -d $(GPMGMT)/doc ]; then cp -rp $(GPMGMT)/doc $(INSTLOC)/docs/cli_help; fi
	if [ -d $(GPMGMT)/demo/gpmapreduce ]; then \
	  mkdir -p $(INSTLOC)/demo; \
	  tar -C $(GPMGMT)/demo -czf $(INSTLOC)/demo/gpmapreduce.tar.gz gpmapreduce; \
	fi
	if [ -d $(GPMGMT)/demo/gpfdist_transform ]; then \
	  mkdir -p $(INSTLOC)/demo; \
	  tar -C $(GPMGMT)/demo -czf $(INSTLOC)/demo/gpfdist_transform.tar.gz gpfdist_transform; \
	fi
	$(MAKE) set_scripts_version INSTLOC=$(INSTLOC)
	# Remove unwanted files.
	rm -rf $(INSTLOC)/bin/CVS
	rm -rf $(INSTLOC)/doc/CVS
	rm -rf $(INSTLOC)/bin/ext
	rm -rf $(INSTLOC)/bin/pythonSrc
	rm -rf $(INSTLOC)/bin/Makefile
	rm -rf $(INSTLOC)/bin/lib/CVS
	rm -rf $(INSTLOC)/bin/lib/.p4ignore
	rm -rf $(INSTLOC)/bin/src
	rm -rf $(INSTLOC)/bin/gppylib
	find $(INSTLOC)/lib/python/gppylib -name test -type d | xargs rm -rf

libcopy=(cd $(1) && tar cf - $(2)) | (cd $(3) && tar xvf -)$(check_pipe_for_errors)

copylibs :
	# Create the python directory to flag to build scripts that python has been handled
	mkdir -p $(INSTLOC)/ext/python
	@if [ ! -z "$(PYTHONHOME)" ]; then \
	    echo "Copying python, ., from $(PYTHONHOME) into $(INSTLOC)/ext/python..."; \
	    (cd $(PYTHONHOME) && tar cf - .) | (cd $(INSTLOC)/ext/python/ && tar xpf -); \
		echo "...DONE"; \
	else \
	    echo "INFO: Python not found on this platform, $(BLD_ARCH), not copying it into the GPDB package."; \
	fi
	# Create the python3.9 directory to flag to build scripts that python has been handled
	mkdir -p $(INSTLOC)/ext/python3.9
	@if [ ! -z "$(PYTHONHOME39)" ]; then \
	    echo "Copying python3.9, ., from $(PYTHONHOME39) into $(INSTLOC)/ext/python3.9..."; \
	    (cd $(PYTHONHOME39) && tar cf - .) | (cd $(INSTLOC)/ext/python3.9/ && tar xpf -); \
		echo "...DONE"; \
	else \
	    echo "INFO: Python3.9 not found on this platform, $(BLD_ARCH), not copying it into the GPDB package."; \
	fi
	mkdir -p $(INSTLOC)/etc
	mkdir -p $(INSTLOC)/include

greenplum_path:
	mkdir -p $(INSTLOC)
	$(BUILDDIR)/gpMgmt/bin/generate-greenplum-path.sh yes > $(INSTLOC)/greenplum_path.sh

copylicense:
	for proddir in $(INSTLOC) $(CLIENTSINSTLOC); do \
	   if [ -d $${proddir} ]; then \
	      echo cp -v ${BLD_TOP}/LICENSE $${proddir}; \
	      echo cp -v ${BLD_TOP}/NOTICE  $${proddir}; \
	   fi; \
	done

HOMEDEP :
	@ #If the HOME variable isn't set, then execute the usage() path
	@if [ x"$(HOME)" = x ]; then $(MAKE) HOMEFAIL; fi

GPROOTDEP :
	@ #If the GPROOT variable isn't set, then execute the usage() path
	@if [ x"$(GPROOT)" = x ]; then $(MAKE) GPROOTFAIL; fi

GPROOTFAIL :
	@echo
	@echo "	The GPROOT path variable is not set."
	@echo
	@echo "	You must decide where you will install to, and set GPROOT to that location, prior to"
	@echo "	installing the GP module with this Makefile."
	@echo
	@exit 1

HOMEFAIL :
	@echo
	@echo "	The HOME environment variable is not set. Please set it to continue."
	@echo
	@exit 1

#---------------------------------------------------------------------
# Cleanup
#---------------------------------------------------------------------

clean :
#	@if [ ! -f $(ISCONFIG) ]; then echo "GPDB root not configured, no need to clean it"; fi
	@if [ -d Debug -a -f Debug/GNUmakefile ]; then cd Debug; $(MAKE) clean; fi
	@if [ -d Release -a -f Release/GNUmakefile ]; then cd Release; $(MAKE) clean; fi
	@if [ -d $(GPPGDIR) -a -f $(GPPGDIR)/GNUmakefile ]; then cd $(GPPGDIR); $(MAKE) clean; fi
	@if [ -d $(GPMGMT)/bin ]; then cd $(GPMGMT)/bin; $(MAKE) clean; fi

distclean :
#	@if [ ! -f $(ISCONFIG) ]; then echo "GPDB root not configured, no need to clean it"; fi
	@if [ -d Debug -a -f Debug/GNUmakefile ]; then cd Debug; $(MAKE) distclean; fi
	@if [ -d Release -a -f Release/GNUmakefile ]; then cd Release; $(MAKE) distclean; fi
	@if [ -d $(GPPGDIR) -a -f $(GPPGDIR)/GNUmakefile ]; then cd $(GPPGDIR); $(MAKE) distclean; fi
	@if [ -d $(GPMGMT)/bin ]; then cd $(GPMGMT)/bin; $(MAKE) distclean; fi
	@rm -f VERSION
	@rm -f env.sh

distprep : $(ISCONFIG)
	@cd $(BUILDDIR) && $(MAKE) distprep

#---------------------------------------------------------------------
# More internal functions
#---------------------------------------------------------------------

gccVersionCheck :
	@echo "Checking GCC version" ; \
	$(BLD_CC) --version > /dev/null 2>&1 ; \
	if [ $$? -ne 0 ] ; then \
	    echo "Error running $(BLD_CC) --version" ; \
	    $(BLD_CC) --version ; \
	    exit 1 ; \
	fi
	@$(BLD_CC) --version | head -n 1 | cut -d' ' -f3 | egrep '^[0-3]\.[0-4]\.[0-1]' > /dev/null 2>&1 ; \
	if [ $$? -eq 0 ] ; then \
	    echo "Error: GPDB must be compiled with GCC 3.4.2 or later." ; \
	    echo "Current GCC version:" ; \
	    $(BLD_CC) --version ; \
	    exit 1 ; \
	fi
